const app = getApp();
var util = require('../../utils/util.js');
var amap = require('../../libs/amap-wx.js');

Page({
    data: {
        isShow: false,
        tips: {},
        longitude: '',
        latitude: '',
        markers: [],

    },
    onLoad() {
        var that = this;
        wx.getLocation({
            success: function(res) {
                if (res && res.longitude){
                    that.setData({
                        longitude: res.longitude,
                        latitude: res.latitude,
                        markers:[{
                            id:0,
                            longitude: res.longitude,
                            latitude: res.latitude,
                            // iconPath: '../../src/images/ding.png',
                            width:32,
                            height:32
                        }]
                    })
                }
            }
        }),
        this.moveToLocation()
    },
    bindInput: function (e) {
        var _this = this;
        var keywords = e.detail.value;
        var myAmap = new amap.AMapWX({ key: app.globalData.aMapKey });
        myAmap.getInputtips({
            keywords: keywords,
            location: '',
            success: function (res) {
                if (res && res.tips) {
                    _this.setData({
                        isShow: true,
                        tips: res.tips
                    });
                }
            }
        })
    },
    bindSearch: function (e) {
        var keywords = e.target.dataset.keywords;
        var location = e.target.dataset.location.split(',');


        this.setData({
            isShow: false,
            longitude: location[0],
            latitude: location[1],
            markers: [{
                id: 0,
                longitude: location[0],
                latitude: location[1],
                // iconPath: '../../src/images/ding.png',
                width: 32,
                height: 32,
                anchor: { x: .5, y: 1 },
                label: {
                    content: keywords,
                    color: 'blue',
                    fontSize: 12,
                    borderRadius: 5,
                    bgColor: '#fff',
                    padding: 3,
                    x: 0,
                    y: -50,
                    textAlign: 'center'
                }
            }]
        })
    },
    moveToLocation: function () {
        var that = this;
        wx.chooseLocation({
            success: function (res) {
                console.log(res.name);
                //选择地点之后返回到原来页面
                wx.navigateTo({
                    url: "/pages/get-maps/get-maps?address="+res.name
                });
            },
            fail: function (err) {
                console.log(err)
            }
        });
    }
})